package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.szholly.pro.watergas_server.ps.monitor.MonitorDataFormater;
import org.springframework.stereotype.Service;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.common.DateTime;
import com.szholly.pro.watergas_server.ps.android.common.NonNullUtil;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
/**
 * 历史数据
 * 
 * 2017/6/12 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
@Service
public class MoveDataHistoryService {
	private final MonitorDataFormater mFormater = MonitorDataFormater.getInstance();
	
	// 获取一天的小时数据 (value,parameterCode,time)
	public List<RowBase> getOneDayHourData(String stationCode, DateTime dateTime) throws Exception {
		ValidUtils.hasText(stationCode, "站点编号为空，无法获取一天的小时数据");
		ValidUtils.notNull(dateTime, "日期为空，无法获取一天的小时数据");
		String dateTimeStr = dateTime.toString("yyyy-MM-dd");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationCode", stationCode));
		parms.add(new GlobalParameter("dateTime", dateTimeStr));
		String sql = "select t.avg \"value\",\n"
				+ "       t.parmname \"parameterCode\",\n"
				+ "       to_char(t.datatime, 'hh24') \"time\"\n"
				+ "  from monitor_data_hour t\n"
				+ "  left join BC_COLLECTION_INFO t1\n"
				+ "    on t1.device_num = t.mn\n"
				+ " where t1.site_id = @stationCode\n"
				+ "   and t.datatime >= to_date(@dateTime, 'yyyy-mm-dd')\n"
				+ "   and t.datatime < to_date(@dateTime, 'yyyy-mm-dd')+1\n"
				+ " order by t.parmname, t.datatime";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		rows = NonNullUtil.toList(rows);
		// 校验数值 如果不属于数值，则删除
		List<RowBase> resultRows = new ArrayList<RowBase>();
		for (RowBase rowBase : rows) {
			try {
				String parameterCode = String.valueOf(rowBase.get("parameterCode"));
				Object value = rowBase.get("value");
				Integer time = Integer.valueOf(rowBase.get("time").toString());
				String formatValue = mFormater.NumPointFormat(parameterCode, value, "0");
				if (time != null && !StringUtils.IsNullOrSpace(formatValue)) {
					rowBase.put("value", formatValue);
					resultRows.add(rowBase);
				}
			} catch (Exception e) {

			}
		}
		return NonNullUtil.toList(resultRows);
	}
	//获取时间段内的历史数据
		public List<RowBase> getHistoryHourData(String stationCode, DateTime startTime,DateTime endTime)throws Exception{
			String startTimeStr = startTime.toString("yyyy-MM-dd");
			String endTimeStr = endTime.toString("yyyy-MM-dd");
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			parms.add(new GlobalParameter("stationCode", stationCode));
			parms.add(new GlobalParameter("startTime", startTimeStr));
			parms.add(new GlobalParameter("endTime", endTimeStr));
			String sql ="select t.avg \"value\",\n" +
							"       t.parmname \"parameterCode\",\n" + 
							"       to_char(t.datatime, 'dd-hh24') \"time\"\n" + 
							"  from monitor_data_hour t\n" + 
							"  left join BC_COLLECTION_INFO t1\n" + 
							"    on t1.device_num = t.mn\n" + 
							" where t1.site_id = @stationCode\n" + 
							"    and t.datatime >= to_date(@startTime, 'yyyy-mm-dd')\n" + 
							"   and t.datatime < to_date(@endTime, 'yyyy-mm-dd')+1\n" + 
							" order by t.parmname, t.datatime";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			rows = NonNullUtil.toList(rows);
			// 校验数值 如果不属于数值，则删除
					List<RowBase> resultRows = new ArrayList<RowBase>();
					for (RowBase rowBase : rows) {
						try {
							String parameterCode = String.valueOf(rowBase.get("parameterCode"));
							Object value = rowBase.get("value");
//							Integer time = Integer.valueOf(rowBase.get("time").toString());
							String formatValue = mFormater.NumPointFormat(parameterCode, value, "0");
							if (!StringUtils.IsNullOrSpace(formatValue)) {
								rowBase.put("value", formatValue);
								resultRows.add(rowBase);
							}
						} catch (Exception e) {

						}
					}
					return NonNullUtil.toList(resultRows);
		}
	// 获取一天的分钟数据 (value,parameterCode,time)
	public List<RowBase> getOneDayMinData(String stationCode, DateTime dateTime) throws Exception {
		ValidUtils.hasText(stationCode, "站点编号为空，无法获取一天的小时数据");
		ValidUtils.notNull(dateTime, "日期为空，无法获取一天的小时数据");
		String dateTimeStr = dateTime.toString("yyyy-MM-dd");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("stationCode", stationCode));
		parms.add(new GlobalParameter("dateTime", dateTimeStr));
		String sql = "select t.avg \"value\",\n"
				+ "       t.parmname \"parameterCode\",\n"
				+ "       to_char(t.datatime, 'hh24:mi') \"time\"\n"
				+ "  from monitor_data_min t\n"
				+ "  left join BC_COLLECTION_INFO t1\n"
				+ "    on t1.device_num = t.mn\n"
				+ " where t1.site_id = @stationCode\n"
				+ "   and t.datatime >= to_date(@dateTime, 'yyyy-mm-dd')\n"
				+ "   and t.datatime < to_date(@dateTime, 'yyyy-mm-dd')+1\n"
				+ " order by t.parmname, t.datatime";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		rows = NonNullUtil.toList(rows);
		// 校验数值 如果不属于数值，则删除
		final Pattern pattern = Pattern.compile("[0-9]{2}:[0-9]{2}");
		List<RowBase> resultRows = new ArrayList<RowBase>();
		for (RowBase rowBase : rows) {
			try {
				String parameterCode = String.valueOf(rowBase.get("parameterCode"));
				Object value = rowBase.get("value");
				String time = rowBase.get("time").toString();
				String formatValue = mFormater.NumPointFormat(parameterCode, value, "0");
				if (pattern.matcher(time).matches() && !StringUtils.IsNullOrSpace(formatValue)) {
					rowBase.put("value", formatValue);
					resultRows.add(rowBase);
				}
			} catch (Exception e) {

			}
		}
		return NonNullUtil.toList(resultRows);
	}

	public List<RowBase> getAlarmLimit(String stationCode) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String sql = "select t.density_alarm_dowmlimit \"downLimit\",\n"
				+ "       t.density_alarm_uplimit   \"upLimit\",\n"
				+ "       t.site_id                 \"stationCode\",\n"
				+ "       t.name                    \"parameterCode\"\n"
				+ "  from BC_SITE_POLL t\n"
				+ " where 1 = 1\n";
		if (!StringUtils.IsNullOrSpace(stationCode)) {
			parms.add(new GlobalParameter("stationCode", stationCode));
			sql += "   and t.site_id = @stationCode\n";
		}
		sql += " order by t.site_id";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}
}
